package com.pc.cloud.services.impl;

import com.pc.cloud.services.DocumentBuilder;
import com.pc.cloud.utils.DocumentUtils;
import org.apache.lucene.document.Document;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: huangqinghe
 * Date: 13-4-25
 * Time: 14 21
 * To change this template use File | Settings | File Templates.
 */
public class DefaultDocumentBuilder implements DocumentBuilder{
    private String searchType;
	private Collection<String> keys = new ArrayList<String>();
	private Collection<Document> docs = new ArrayList<Document>();
    
    public DefaultDocumentBuilder(JSONObject pd) throws Exception {
		super();
		this.searchType = pd.getString("searchType");
		JSONArray ks = pd.getJSONArray("keys");
		List<String> keys = new ArrayList<String>();
		for(int i=0;i<ks.length();i++) {
			keys.add(ks.getString(i));
		}
		this.keys = keys;

		JSONArray ds = pd.getJSONArray("docs");
		List<Document> docs = new ArrayList<Document>();
		for(int i=0;i<ds.length();i++) {
			docs.add(DocumentUtils.jsonToDoc(ds.getJSONObject(i)));
		}
		this.docs = docs;
	}
    @Override
    public String getSearchType() {
        return this.searchType;
    }

    @Override
    public Collection<Document> getDocs() throws Exception {
        return this.docs; 
    }

    @Override
    public Collection<String> getKeys() throws Exception {
        return this.keys;  //To change body of implemented methods use File | Settings | File Templates.
    }
    

}
